//
//  EventModelInMonthView.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/8.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit
import Realm

class DayInMonthView {
    
    var today = Array<Int>(count: 8, repeatedValue: 0)
    var next = Array<Int>(count: 8, repeatedValue: 0)
    
    var reload = 0
    
    func move() {
        self.today = self.next
        self.next = Array<Int>(count: 8, repeatedValue: 0)
        self.reload = 0
    }
    
    func getRemainNumber(max: Int) -> Int {
        var count = 0
        if max > 7 {
            return self.reload
        }
        for i in max...7 {
            if (today[i] != 0) {
                count = count + 1
            }
        }
        return count + self.reload
    }
    
    func insert(localId: Int, isFinished: Bool) {
        // todo: 可以优化这里的步骤
        if (self.check(localId)) {
            if (!isFinished) {
                for i in 0...7 {
                    if (self.today[i] == localId) {
                        self.next[i] = localId
                    }
                }
            }
            return
        } else {
            var j = 0
            for i in 0...7 {
                if (self.today[i] == 0) {
                    self.today[i] = localId
                    if (!isFinished) {
                        self.next[i] = localId
                    }
                    return
                }
                j = j + 1
            }
            if j == 8 {
                reload = reload + 1
            }
        }
    }
    
    func check(localId: Int) -> Bool {
        for i in self.today {
            if (i == localId) {
                return true
            }
        }
        return false
    }
    
    func isFirst(localId: Int) -> Bool {
        var sum = 0
        for i in 0...7 {
            sum = sum + self.today[i]
            if (sum != 0 && sum == localId) {
                return true
            } else {
                return false
            }
        }
        return false
    }
    
    func isLast(localId: Int) -> Bool {
        var sum = 0
        for i in (0...7).reverse() {
            sum = sum + self.today[i]
            if (sum != 0 && sum == localId) {
                return true
            } else {
                return false
            }
        }
        return false
    }
    
    func lineType(localId: Int) -> CellLineType {
        let k = lineNum(localId)
        var sum1 = 0
        for i in (0...k) {
            sum1 = sum1 + self.today[i]
        }
        var sum2 = 0
        for i in (k...7) {
            sum2 = sum2 + self.today[i]
        }
        if (sum2 == sum1 && sum1 == localId) {
            return CellLineType.LineNone
        } else if (sum1 == localId) {
            if (k < 7 && self.today[k+1] == 0) {
                return CellLineType.LineNone
            }
            return CellLineType.LineFirst
        } else if (sum2 == localId) {
            if (k > 0 && self.today[k-1] == 0) {
                return CellLineType.LineNone
            }
            return CellLineType.LineLast
        } else {
            if (self.today[k-1] == 0 && self.today[k+1] == 0) {
                return CellLineType.LineNone
            } else if (self.today[k-1] == 0) {
                return CellLineType.LineFirst
            } else if (self.today[k+1] == 0) {
                return CellLineType.LineLast
            } else {
                return CellLineType.LineMiddle
            }
        }
    }
    
    func lineNum(localId: Int) -> Int {
        for i in 0...7 {
            if (self.today[i] == localId) {
                return i
            }
        }
        return -1
    }
}

class EventInMonthView: NSObject {
    
    var eventColorType = 4 // 5 & nil
    
    var eventCircleType : CircleIconType = CircleIconType.IconNone
    
    var eventBarType : BarType = BarType.BarNone
    
    var eventLineType : CellLineType = CellLineType.LineNone
    
    var eventTitle = ""
    
    var eventLocalId = 0
    
    var isShowTitle = true
    
    override init() {
        
    }
    
}
